package genomemap.util;

import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class Util {

    public static final String IMAGE_FORMAT_JPEG = "jpeg";

    public static final String IMAGE_FORMAT_PNG = "png";

    public static void saveImage(File file, String format, JComponent comp,
            int width, int height) {
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g2 = image.createGraphics();
        comp.paint(g2);
        g2.dispose();
        try {
            ImageIO.write(image, format, file);
        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }
    }

    public static int[] randomBlockReversal(int[] oldOrder) {
        int[] newOrder = (int[]) oldOrder.clone();

        // choose 2 end points (i and j) randomly on the length of the order
        int i = (int) (Math.random() * newOrder.length);
        int j = (int) (Math.random() * newOrder.length);

        i = Math.min(i, j);
        j = Math.max(i, j);

        // reverse the block between i and j, including i and j.
        for (int k = i; k <= j; k++) {
            newOrder[k] = oldOrder[j - k + i];
        }

        return newOrder;
    }

    public static int findMax(int[] array) {
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    public static int findMin(int[] array) {
        int min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        return min;
    }

    public static void showFrame(Component comp) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.add(comp);

        frame.setLocation(100,100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(panel);

        frame.pack();
        frame.setVisible(true);
    }

    public static List<Integer> createSeq(int n) {

        List<Integer> seq = new ArrayList<Integer>(n);

        for (int i = 0; i < n; i++) {
            seq.add(i+1);
        }
        
        return seq;
    }

    public static class IO {

        public static void writeToFile(File file, String str) {

            PrintWriter outFile = null;

            try {
                outFile = new PrintWriter(new BufferedWriter(new FileWriter(file,
                        false)));
            } catch (IOException e) {

                e.printStackTrace();
            }
            outFile.print(str);
            outFile.close();

        }

    }
}
